
import { parse } from 'qs'
import { message } from 'antd'
import moment from 'moment'
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
import { hashHistory } from 'dva/router'
import { regionArea,getProduct,queryAllAddress,houseListGet,addServer,delServer,projectDetail} from './integralExchangeNoticeServ.js'
import { makeAddress } from "utils/util"

const defultState = {
    biz:{},
    area1:[],
    areaList:[],
    areaList2: [],//省市区列表
    loading:false,
    roleList:[],
    showModal:false,
    userInfo:{},
    professionalBroker:{},
    detail:{
      mobileArr:['']
    },
    estateList:[],
    actionType:'',
}

const tmpModule = {
  state: cloneDeep(defultState),
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({ type: 'regionArea', payload:{}});
					dispatch({ type: 'getAreaList', payload: {} });
          dispatch({type: 'updateStore', payload: cloneDeep(defultState)})
        }
      })
    },
  },


  effects: {
    *getAreaList({ payload }, { put, call, select }) {
			try {
				const { success, data } = yield call(queryAllAddress, {})
				if (success) {
					//todo: 接口格式调整后删除
					let areaList = []
					let arr = [data[0]]
					let attr = [data[data.length - 1]]
					data[0] = {
						label: "北京",
						value: "110100",
						children: arr
					}
					data[data.length - 1] = {
						label: "澳门",
						value: "820000",
						children: attr
					}
					//console.log(data,"data")
					//去掉第三级，
					areaList = makeAddress(data, 2, false)
					yield put({ type: 'updateStore', payload: { areaList2:areaList } })
				}
			} catch (e) {
			}
    },
    // 获取户型项目
		*getHouseArea({ payload }, { put, call, select }) {
			let params = cloneDeep(payload)
			params.pageNo = 1
			params.pageSize = 999999
			try {
				const { success, rows } = yield call(houseListGet, params)
				if (success) {
					yield put({ type: 'updateStore', payload: { estateList: rows || [] } })
				}
			} catch (e) {
				console.log(e)
			}
		},
    // *regionArea({ payload },{ put, call, select }){
    //   let {biz,areaList} = yield select(defultState => defultState[tmpModule.namespace])
    //   const { data, success } = yield call(regionArea);
    //   if(success) {
    //     if(biz.queryForm && biz.queryForm.regionId){
    //       data.map((item,index)=>{
    //         if(biz.queryForm && biz.queryForm.regionId && biz.queryForm.regionId==item.value)
    //         {   
    //           areaList=item.children
    //         }
    //       })
    //     }
    //     yield put({ type: 'updateStore',payload:{area1:data,areaList}})
    //   }
    // },
    // *getProduct({payload},{put,call,select}){
    //   let {biz,products} = yield select(defultState => defultState[tmpModule.namespace])
    //   const { data, success } = yield call(getProduct,payload);
    //   if(success){
    //     products=data
    //     yield put({ type: 'updateStore',payload:{products}})
    //   }
    // },
    *delete({ payload }, { put, call, select }){
      let { biz } = yield select((defultState) => defultState[tmpModule.namespace])
      const { success,msg } = yield call(delServer, { ids:payload.ids});
      if(success){
        message.success('删除成功！')
        biz.refreshListDatas()
      }
    },
    *detail({ payload }, { put, call, select }){
      let { detail,biz } = yield select((defultState) => defultState[tmpModule.namespace])
      const { success,data } = yield call(projectDetail, { id:payload.id});
      data.mobileArr=data.mobile.split(",")
      if(success){
        yield put({ type: 'updateStore',payload:{detail:data,showModal:true}})

        // message.success('删除成功！')
        // biz.refreshListDatas()
      }
    },
    //提交表单
		*submit({ payload }, { put, call, select }) {
			try {
				yield put({ type: 'updateStore', payload: { btnLoading: true } })
        let { detail,biz,actionType} = yield select((defultState) => defultState[tmpModule.namespace])
        detail.type = actionType
        detail.mobile=detail.mobileArr.join(',')
			  const { success,msg } = yield call(addServer, { ...detail });
        if(success){
          message.success(`${actionType=='add'?'新增':'编辑'}成功！`)
          biz.refreshListDatas()
          yield put({ type: 'updateStore', payload: { showModal: false,actionType:"",detail:{mobileArr:['']}} })
        }else{
          // message.warning(msg)
        }
				yield put({ type: 'updateStore', payload: { btnLoading: false } })
				
			} catch (e) {
				console.log(e)
			}

		},
  },
  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
